package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;
    @Override
    public Map<String, Object> getCustomerList(Integer page, Integer rows, String customerName) {
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,rows);

        Integer total = customerDao.selectCustomerCount(customerName);
        List<Customer> customerList = customerDao.selectCustomerList(customerName);
        map.put("total",total);
        map.put("rows",customerList);
        return map;
    }

    @Override
    public ServiceVO saveOrUpdateCustomer(Customer customer) {
        if(customer.getCustomerId() == null ){
            if(customerDao.getcustomer(customer.getCustomerName()) != null){
                return new ServiceVO(200,"该客户已存在");
            }
            customerDao.saveCustomer(customer);
        }else {
            customerDao.updateCustomer(customer);
        }

            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);


    }

    @Override
    public ServiceVO deleteCustomer(String ids) {
        String[] split = ids.split(",");

        Integer count = customerDao.deleteCustomer(split);

        if (count>0){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }
        return null;
    }
}
